package com.techgene.kanban.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.techgene.kanban.dao.UserDao;
import com.techgene.kanban.domain.User;
import com.techgene.kanban.exception.KBDaoException;
import com.techgene.kanban.util.AppConstants;


@Component
public class UserDaoImpl implements UserDao {

	
	private Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
	
	@Autowired
	DataSource dataSource;

	
	
	
     /**
	 * inserting user details
	 * @param username
	 * @param password
	 * @param firstname
	 * @param lastname
	 * @param email
	 * @param role
	 * and these details are stored into the database. 
	 * @return either success or failure as a string.
	 * @throws KBDaoException
	 */
	public String storeUserData(String userName,String password,String firstName,String lastName,String email,String role) throws KBDaoException {
		
		logger.debug(">> storeUserData [userName = {},password = {},firstName = {}, lastName = {},email={},role={}]",userName,password,firstName,lastName,email,role);
		String status = null;
		
          try{
        	
        	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        	
        	/*
        	 * calls update method upon jdbcTemplate to store the data into the database. 
        	 */
        	int result =  jdbcTemplate.update(AppConstants.INSERT_USER_DETAILS_QUERY,userName,password,firstName,lastName,email,role);
        	
        	if(result == 1)
        	{
        	  status="success";
        	  logger.debug("status:{}",status);
        	  
        	}else
        	{
        		status = "failure";
        		logger.debug("status:{}",status);
        	}
        	
        	
        	
        }catch(Exception e) {
        	
        	/* for any other errors*/
        	throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_ADD_USER_DETAILS.code,
					AppConstants.ERROR.DAO_UNKNOWN_ERROR_ADD_USER_DETAILS.msg, e);
        	
        }catch(Throwable e){	
        	
        	/* for any other errors*/ 
			throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_ADD_USER_DETAILS.code,
					AppConstants.ERROR.DAO_UNKNOWN_ERROR_ADD_USER_DETAILS.msg, e);
		}
        
        logger.debug("<< storeUserData");
        return status;       
	
	}
	

	
	
	/**
	 * retrieving user details by user name
	 * @param userName
	 * @return profile
	 * @throws KBDaoException
	 */
     public User fecthUserDetails(String userName)throws KBDaoException {
		logger.debug(" >> fetchUserDetails [ userName = {} ] " , userName);
		
		User  profile;
		try
		{
			JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
			String query = AppConstants.SELECT_USER_DETAILS_QUERY;
			
			logger.info(query);
			
			List result =(ArrayList) jdbcTemplate.queryForList(query,userName);
			logger.debug("result:{}",result);
			
			HashMap map =(HashMap)result.get(0);
			
			String username = (String)map.get("username");
			String firstName = (String)map.get("firstname");
			String lastName = (String)map.get("lastname");
			String email = (String)map.get("email");
			String role = (String)map.get("role");
			
		    profile = new User();
			profile.setUsername(username);
			profile.setFirstName(firstName);
			profile.setLastName(lastName);
			profile.setEmail(email);
			profile.setRole(role);
			
		
		}catch (Exception e) {
			
			throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
					AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
			
		}catch (Throwable e) {
			
			throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
					AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
		}
		
		logger.debug(" << fetchUserDetails [ userName = {} ] " , userName);

		return profile;
		
	}
	
	

	
	/**
	 * updating details of user to database
	 */
	 public String updateUserDetails(String userName,String firstName,String lastName,String email,String role) throws KBDaoException{
	    
		 logger.debug(">> updateUserDetails [userName = {},firstName = {}, lastName = {},email={},role={}]",userName,firstName,lastName,email,role);
	    	
		    String status = null;
		
	    	try
	    	{
	    		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		    	String query = "update tbl_user set firstname=?,lastname=?,email=?,role=? where username=?";
		    	logger.info(query);
		    	int result = jdbcTemplate.update(query,firstName,lastName,email,role,userName);
		    	
		    	if(result==1)
		    	{
		    		status="success";
		    	}
		    	else
		    	{
		    		status="failure";
		        }
	    	}catch (Exception e) {
				throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
						AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
			}catch (Throwable e) {
				throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
						AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
			}



	    	logger.debug(" << updateUserDetails");

	    	return status;
	    }
	
	    
	    
	    
	 
	    /**
		 * deleting details of user from database
		 * @param username String
		 * @return
		 */
	     public String deleteUserDetails(String username)throws KBDaoException {
			
	    	logger.debug(" >> deleteUserDetails [username ={}]", username);
	    	
			String status = null;
			try
			{
				
				JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
				
				String query ="delete from tbl_user where username=?"  ;
				logger.info(query);
				
				
				int result = jdbcTemplate.update(query,username);
				
				if(result==1)
		    	{
		    		status="success";
		        }
		    	else
		    	{
		    		status="failure";
		        }
			}catch (Exception e) {
				
				throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
						AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
				
			}catch (Throwable e) {
				
				throw new KBDaoException(AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.code,
						AppConstants.ERROR.DAO_UNKNOWN_ERROR_UDPATE_DELETE_USER_DETAILS.msg, e);
				
			}
			
			logger.debug(" << deleteUserDetails", username);
			return status;        
		      
	    }

}
